package yl.hs.bmipfull.controllers.salemanage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.daos.system.IUserInfoDao;
import yl.hs.bmipfull.pojo.salemanage.SaleContractEntity;
import yl.hs.bmipfull.service.salemanage.ISaleContractProductsService;
import yl.hs.bmipfull.service.salemanage.ISaleContractService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.utils.datastate.ContractState;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;
import yl.hs.bmipfull.viewmodels.salemanmage.TruckPlateModel;

import java.util.*;

/**
 * ContractController
 * @category
 * @author 谢景广
 * @date 2021-05-18
 */
@Api(value = "销售合同Web接口",tags = "销售管理——销售合同管理")
@RequestMapping("/api/contract")
@RestController
public class ContractController extends ControllerBase {
    @Autowired
    ISaleContractService service;
    @Autowired
    ISearchViewService svSVC;
    @Autowired
    ISaleContractProductsService productsService;

    @ApiOperation(value = "查询销售合同信息",notes = "查询销售合同信息")
    @RequestMapping(value = "/info",method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery){
        try {
            return success("查询成功",service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "查询销售合同信息",notes = "查询销售合同信息")
    @RequestMapping(value = "/info/bystate",method = RequestMethod.GET)
    public WebResult queryListByState(){
        try {
            PageQuery pagequery = new PageQuery();
            Map<String,Object> map = new HashMap<>();
            map.put("state", ContractState.Signed);
            pagequery.setQuery(map);
            return success("查询成功",service.queryList(pagequery));
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "查询销售合同信息",notes = "分页查询销售合同信息")
    @RequestMapping(value = {"info/{rowNum}/{page}/{productCateSn}","/info/{rowNum}/{page}"},method = RequestMethod.POST)
    @ResponseBody
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum,
                               @PathVariable(required = false) String productCateSn){
        try {
            query.getQuery().put("productCateSN",productCateSn);
            var result = service.queryPage(query,rowNum,page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "查询全部销售合同信息",notes = "查询全部销售合同信息")
    @RequestMapping(value = {"infoall/{rowNum}/{page}/{productCateSn}","/infoall/{rowNum}/{page}"},method = RequestMethod.POST)
    @ResponseBody
    public WebResult queryPageAll(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum,
                               @PathVariable(required = false) String productCateSn){
        try {
            query.getQuery().put("productCateSN",productCateSn);
            var result = service.queryPageAll(query,rowNum,page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "查询销售合同信息",notes = "根据销售合同信息编号查询销售合同信息")
    @RequestMapping(value = "/info/{sn}",method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn){
        return success("查询成功",service.findBySn(sn));
    }
    @ApiOperation(value = "查询销售合同明细信息",notes = "根据销售合同明细信息编号查询销售合同信息")
    @RequestMapping(value = "/info/detailed/{sn}",method = RequestMethod.GET)
    public WebResult findDetailedBySn(@ApiParam(value = "编号") @PathVariable String sn){
        return success("查询成功",service.queryDetailedBySN(sn));
    }


    @ApiOperation(value = "保存销售合同信息",notes = "查询/更新销售合同信息信息")
    @RequestMapping(value = "/info",method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel){
        Integer  result;
        try {
            result = service.saveSaleContract(submitModel.getData(),submitModel.getSn());
            if (result > 0){
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除销售合同信息信息",notes = "根据销售合同信息编号删除系销售合同信息信息")
    @RequestMapping(path = "/info", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "销售合同信息编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn){
        if (service.delete(sn) > 0){
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }
    @ApiOperation(value = "根据产品查询合同信息",notes = "根据产品查询合同信息")
    @RequestMapping(value = "/querybyproductsn/{sn}",method = RequestMethod.GET)
    public WebResult querySaleByProductSn(@ApiParam(value = "产品种类编号") @PathVariable String sn){
        var ss = sn.split(",");
        var ListSn = new ArrayList<String>();
        for (String s : ss) {
            ListSn.addAll(productsService.querySaleByProductSn(s));
        }
        var result = service.queryBylistSn(ListSn);
        return  success("查询成功",result);
    }
    @ApiOperation(value = "根据合同编号修改合同状态", notes = "根据合同编号修改合同状态")
    @RequestMapping(value = "/info/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult changeStateBySn(@PathVariable String sn,@PathVariable short state) {
        try {
            Integer result = service.changeBySn(sn.split(","), state);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "暂停合同", notes = "暂停合同")
    @RequestMapping(value = "Suspend/{sn}/{remark}", method = RequestMethod.POST)
    @ResponseBody
    public WebResult Suspend(@PathVariable String sn,@PathVariable String remark) {
        try {
            var user= HsApp.getCurrent();
            Integer result = service.changeBySn(sn.split(","), ContractState.Suspend.getCode() );
            service.writeLog(ActionType.UPDATE,sn,user.getUserName()+"暂停了合同；暂停理由："+remark,"", SaleContractEntity.class);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "结束合同", notes = "结束合同")
    @RequestMapping(value = "End/{sn}/{remark}", method = RequestMethod.POST)
    @ResponseBody
    public WebResult End(@PathVariable String sn,@PathVariable String remark) {
        try {
            var user= HsApp.getCurrent();
            Integer result = service.changeBySn(sn.split(","), ContractState.End.getCode());
            service.writeLog(ActionType.UPDATE,sn,user.getUserName()+"结束了合同；结束理由："+remark,"", SaleContractEntity.class);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "重新启动合同", notes = "重新启动合同")
    @RequestMapping(value = "Restart/{sn}/{remark}", method = RequestMethod.POST)
    @ResponseBody
    public WebResult Restart(@PathVariable String sn,@PathVariable String remark) {
        try {
            var user= HsApp.getCurrent();
            Integer result = service.changeBySn(sn.split(","), ContractState.Signed.getCode());
            service.writeLog(ActionType.UPDATE,sn,user.getUserName()+"重新启动了合同；启动原因："+remark,"", SaleContractEntity.class);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "更新合同绑定车辆信息",notes = "根据销售合同信息编号更新合同绑定车辆信息")
    @RequestMapping(path = "/updatetruckplate", method = RequestMethod.POST)
    public WebResult Updatetruckplate(@RequestBody TruckPlateModel truckplate){
       var result =  service.updateTruckPlate(truckplate.getTruckplate(),truckplate.getSn());
        if (result==null || result > 0){
            return success("操作成功");
        } else {
            return fail("操作失败");
        }
    }
}
